package cn.bob.product.service.impl;

import cn.bob.basic.config.BussinessException;
import cn.bob.basic.query.BaseQuery;
import cn.bob.basic.utils.CodeGenerateUtils;
import cn.bob.basic.utils.PageList;
import cn.bob.order.domain.OrderProduct;
import cn.bob.order.mapper.OrderProductMapper;
import cn.bob.org.domain.Employee;
import cn.bob.org.domain.Shop;
import cn.bob.org.mapper.ShopMapper;
import cn.bob.product.domain.Product;
import cn.bob.product.domain.ProductStaycheck;
import cn.bob.product.dto.ProductDto;
import cn.bob.product.mapper.ProductMapper;
import cn.bob.product.mapper.ProductStaycheckMapper;
import cn.bob.product.service.IProductStaycheckService;
import cn.bob.basic.service.impl.BaseServiceImpl;
import cn.bob.user.domain.User;
import cn.bob.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2022-11-04
 */
@Service
public class ProductStaycheckServiceImpl extends BaseServiceImpl<ProductStaycheck> implements IProductStaycheckService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductStaycheckMapper productStaycheckMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;

    @Override
    public PageList<ProductStaycheck> queryByPage(BaseQuery query) {
        //添加user用户和店铺
        PageList<ProductStaycheck> productStaycheckPageList = super.queryByPage(query);
        List<ProductStaycheck> rows = productStaycheckPageList.getRows();
        rows.forEach(productStaycheck -> {
            Long shopId = productStaycheck.getShopId();
            Shop shop = shopMapper.findById(shopId);
            Long userId = productStaycheck.getUserId();
            User user = userMapper.findById(userId);
            productStaycheck.setShop(shop);
            productStaycheck.setUser(user);
        });
        Integer total = productStaycheckPageList.getTotal();
        return  new PageList<>(rows,total);
    }

    /**
     * 通过前台前端传入的id生成待处理表
     * @param id
     * @param userId
     */
    @Override
    public void staycheck(Long id,Long userId) {
        Product product = productMapper.findById(id);
        ProductStaycheck productStaycheck = new ProductStaycheck();
        productStaycheck.setName(product.getName());
        productStaycheck.setSaleprice(product.getSaleprice());
        productStaycheck.setShopId(product.getShopId());
        productStaycheck.setState(0L);
        productStaycheck.setUserId(userId);
        productStaycheck.setProductId(id);
        productStaycheckMapper.add(productStaycheck);
    }

    /**
     * 接单生成订单消息
     * @param productDto
     */
    @Override
    public void accept(ProductDto productDto,Long empId) {
        //接单
        if(productDto.getNote()==null || productDto.getStayId()==null || productDto.getStayId()==null){
            throw new BussinessException("参数不能为空！！");
        }
        //接单成功更改状态
        ProductStaycheck productStaycheck = productStaycheckMapper.findById(productDto.getStayId());
        productStaycheck.setState(1L);
        productStaycheckMapper.update(productStaycheck);

        //生成订单消息
        //需添加数据 id为服务的id
        Product product = productMapper.findById(productStaycheck.getProductId());
        //存放到接单中
        //将状态改为0待支付
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setDigest("购买了一次"+product.getName());
        orderProduct.setState(0);
        orderProduct.setPrice(product.getSaleprice());
        orderProduct.setOrderSn(CodeGenerateUtils.generateOrderSn(productStaycheck.getProductId()));
        orderProduct.setLastPayTime(new Date(System.currentTimeMillis()+24*60*60*1000));
        orderProduct.setNum(1);
        orderProduct.setProductId(product.getId());
        orderProduct.setUserId(empId);
        orderProduct.setShopId(product.getShopId());
        orderProductMapper.add(orderProduct);
    }

    /**
     * todo 拒单生成拒单日志
     * @param stayId
     */
    @Override
    public void reject(Long stayId,Long empId) {
        if (stayId==null){
            throw new BussinessException("拒单的id不能为空！！");
        }
        ProductStaycheck productStaycheck = productStaycheckMapper.findById(stayId);
        //状态置为-1拒单成功 生成日志
        productStaycheck.setState(-1L);

        productStaycheckMapper.update(productStaycheck);
        //todo 生成拒单日志
    }
}
